package cn.poka.util;


public class Tools {

	public static byte[] ivalBack;

	public static  int fingerCount = 0;

	// byte 转十六进制
	public static String Bytes2HexString(byte[] b, int size) {
		String ret = "";
		for (int i = 0; i < size; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = "0" + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}

	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
				.byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
				.byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}

	// 十六进制转byte
	public static byte[] HexString2Bytes(String src) {
		int len = src.length() / 2;
		byte[] ret = new byte[len];
		byte[] tmp = src.getBytes();

		for (int i = 0; i < len; i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}

	/* byte[]转Int */
	public static int bytesToInt(byte[] bytes) {
		int addr = bytes[0] & 0xFF;
		addr |= ((bytes[1] << 8) & 0xFF00);
		addr |= ((bytes[2] << 16) & 0xFF0000);
		addr |= ((bytes[3] << 25) & 0xFF000000);
		return addr;
	}

	public static byte[] shortToByte(short a) {
		byte[] b = new byte[2];
		b[0] = (byte) ((a & 0xff00) >> 8);
		b[1] = (byte) (a & 0x00ff);
		return b;
	}

	public static short byteToShort(byte[] b) {
		return (short) ((short) (b[1] & 0x00ff) + (short) ((b[0] & 0x00ff) << 8));
	}

	/* Int转byte[] */
	public static byte[] intToByte(int i) {
		byte[] abyte0 = new byte[4];
		abyte0[0] = (byte) (0xff & i);
		abyte0[1] = (byte) ((0xff00 & i) >> 8);
		abyte0[2] = (byte) ((0xff0000 & i) >> 16);
		abyte0[3] = (byte) ((0xff000000 & i) >> 24);
		return abyte0;
	}

//	public static void toastShow(String string, Context context,int showTime) {
//		Toast toast = new Toast(context);
//		LayoutInflater inflater = LayoutInflater.from(context);
//		View view = inflater.inflate(R.layout.toast, null);
//		TextView text = (TextView) view.findViewById(R.id.toast);
//		text.setText(string);
//		toast.setView(view);
//		toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
//		//toast.setDuration(Toast.LENGTH_SHORT);
//		toast.setDuration(showTime);
//		toast.show();
//	}

}
